import React, { useEffect } from "react";
// import { useNavigate } from "react-router-dom";
import { useLocation, Navigate } from "react-router-dom";

interface Props {
  children: React.ReactElement;
}

export default function Auth({ children }: Props) {
  const { pathname } = useLocation();
  const isAuth = localStorage.getItem("token");

  if (pathname === "/login") {
    if (isAuth && Number(isAuth) !== 1) {
      return <Navigate to="/" />;
    } else {
      return <>{children}</>; // 写成：return <Navigate to="/login" />会造成无限循环
    }
  }

  if (isAuth && Number(isAuth) !== 1) {
    return <>{children}</>;
  } else {
    return <Navigate to="/login" />;
  }
}
